from pyecharts import options as opts
from pyecharts.charts import Grid, Line
import pandas as pd

def toInt(x):
  return int(x)
def toFloat(x):
  return float(x)
def removeYear(x):
  return str(x)[5:]

def AQI(df):
    t2014 = df[df["日期"]<"2015"]
    t2015 = df[(df["日期"]>="2015") & (df["日期"]<"2016")]
    t2016 = df[(df["日期"]>="2016") & (df["日期"]<"2017")]
    t2017 = df[(df["日期"]>="2017") & (df["日期"]<"2018")]
    t2018 = df[(df["日期"]>="2018") & (df["日期"]<"2019")]
    t2019 = df[(df["日期"]>="2019") & (df["日期"]<"2020")]
    t2020 = df[(df["日期"]>="2020") & (df["日期"]<"2021")]
    t2021 = df[(df["日期"]>="2021") & (df["日期"]<"2022")]
    
    date = map(removeYear, t2014["日期"].tolist())
    # print(date)

    PM2p5_2014 = map(toInt, t2014["AQI"].tolist())
    PM2p5_2015 = map(toInt, t2015["AQI"].tolist())
    PM2p5_2016 = map(toInt, t2016["AQI"].tolist())
    PM2p5_2017 = map(toInt, t2017["AQI"].tolist())
    PM2p5_2018 = map(toInt, t2018["AQI"].tolist())
    PM2p5_2019 = map(toInt, t2019["AQI"].tolist())
    PM2p5_2020 = map(toInt, t2020["AQI"].tolist())
    PM2p5_2021 = map(toInt, t2021["AQI"].tolist())
    
    line = (
      Line()
      .add_xaxis(list(date))
      .add_yaxis("AQI_2014", list(PM2p5_2014))
      .add_yaxis("AQI_2015", list(PM2p5_2015))
      .add_yaxis("AQI_2016", list(PM2p5_2016))
      .add_yaxis("AQI_2017", list(PM2p5_2017))
      .add_yaxis("AQI_2018", list(PM2p5_2018))
      .add_yaxis("AQI_2019", list(PM2p5_2019))
      .add_yaxis("AQI_2020", list(PM2p5_2020))
      .add_yaxis("AQI_2021", list(PM2p5_2021))
    )

    grid = (
      Grid()
      .add(line, grid_opts=opts.GridOpts())
      .render("gridAQI.html")
    )

def PM2p5(df):
    t2014 = df[df["日期"]<"2015"]
    t2015 = df[(df["日期"]>="2015") & (df["日期"]<"2016")]
    t2016 = df[(df["日期"]>="2016") & (df["日期"]<"2017")]
    t2017 = df[(df["日期"]>="2017") & (df["日期"]<"2018")]
    t2018 = df[(df["日期"]>="2018") & (df["日期"]<"2019")]
    t2019 = df[(df["日期"]>="2019") & (df["日期"]<"2020")]
    t2020 = df[(df["日期"]>="2020") & (df["日期"]<"2021")]
    t2021 = df[(df["日期"]>="2021") & (df["日期"]<"2022")]
    
    date = map(removeYear, t2014["日期"].tolist())
    # print(date)

    PM2p5_2014 = map(toInt, t2014["PM2.5"].tolist())
    PM2p5_2015 = map(toInt, t2015["PM2.5"].tolist())
    PM2p5_2016 = map(toInt, t2016["PM2.5"].tolist())
    PM2p5_2017 = map(toInt, t2017["PM2.5"].tolist())
    PM2p5_2018 = map(toInt, t2018["PM2.5"].tolist())
    PM2p5_2019 = map(toInt, t2019["PM2.5"].tolist())
    PM2p5_2020 = map(toInt, t2020["PM2.5"].tolist())
    PM2p5_2021 = map(toInt, t2021["PM2.5"].tolist())
    
    line = (
      Line()
      .add_xaxis(list(date))
      .add_yaxis("PM2.5_2014", list(PM2p5_2014))
      .add_yaxis("PM2.5_2015", list(PM2p5_2015))
      .add_yaxis("PM2.5_2016", list(PM2p5_2016))
      .add_yaxis("PM2.5_2017", list(PM2p5_2017))
      .add_yaxis("PM2.5_2018", list(PM2p5_2018))
      .add_yaxis("PM2.5_2019", list(PM2p5_2019))
      .add_yaxis("PM2.5_2020", list(PM2p5_2020))
      .add_yaxis("PM2.5_2021", list(PM2p5_2021))
    )

    grid = (
      Grid()
      .add(line, grid_opts=opts.GridOpts())
      .render("gridPM2.5.html")
    )

def _2021(df):
    t2021 = df[(df["日期"]>="2021") & (df["日期"]<"2022")]
    
    date = t2021["日期"].tolist()
    # print(date)

    PM2p5 = map(toInt, t2021.loc[:, "PM10"].tolist())
    PM10 = map(toInt, t2021.loc[:, "PM10"].tolist())
    SO2 = map(toInt, t2021.loc[:, "SO2"].tolist())
    CO = map(toFloat, t2021.loc[:, "CO"].tolist())
    NO2 = map(toInt, t2021.loc[:, "NO2"].tolist())
    O3 = map(toInt, t2021.loc[:, "O3_8h"].tolist())
    
    line = (
      Line()
      .add_xaxis(list(date))
      .add_yaxis("PM2.5", list(PM2p5))
      .add_yaxis("PM10", list(PM10))
      .add_yaxis("SO2", list(SO2))
      .add_yaxis("CO", list(CO))
      .add_yaxis("NO2", list(NO2))
      .add_yaxis("O3", list(O3))
    )

    grid = (
      Grid()
      .add(line, grid_opts=opts.GridOpts())
      .render("grid2021.html")
    )

def main():
    # print(removeYear("2013-10-1"))
    excel = pd.read_excel(r'成都空气质量.xlsx') # 读取所有数据
    df = pd.DataFrame(excel)
    # _PM2p5(df)
    # _2021(df)
    AQI(df)

main()